package com;

import java.util.Comparator;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

public class Collection_15_SortedMap_01 {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		@SuppressWarnings("unchecked")
		SortedMap products = new TreeMap(new Comparator() {

			@Override
			public int compare(Object o1, Object o2) {
				double price1 = ((Product) o1).price;
				double price2 = ((Product) o2).price;
				if (price1 == price2) {
					return 0;
				} else if (price1 < price2) {
					return -1;
				} else {
					return 1;
				}
				// return Double.compare(price1, price2);
			}

		});
		Product p1 = new Product("water", 1.0);
		Product p2 = new Product("苹果", 3.0);
		Product p3 = new Product("香蕉", 2.0);
		Product p4 = new Product("梨", 1.5);

		// value 可以设置为已购买的数量
		products.put(p1, 4.0);
		products.put(p2, 2.0);
		products.put(p3, 1.0);
		products.put(p4, 4.0);

		Set keys = products.keySet();
		for (Object key : keys) {
			Product p = (Product) key;
			double value = (double) products.get(key);
			System.out.println(p + "->" + value + "kg 总价 = "
					+ (value * p.price));
		}
		// Product [name=water, price=1.0]->4.0kg 总价 = 4.0
		// Product [name=梨, price=1.5]->4.0kg 总价 = 6.0
		// Product [name=香蕉, price=2.0]->1.0kg 总价 = 2.0
		// Product [name=苹果, price=3.0]->2.0kg 总价 = 6.0

	}
}

class Product {
	String name;
	double price;

	public Product(String name, double price) {
		super();
		this.name = name;
		this.price = price;
	}

	@Override
	public String toString() {
		return "Product [name=" + name + ", price=" + price + "]";
	}
}